"""

1. flask-session
    作用：将默认保存的签名cookie中的值 保存到 redis/memcached/file/Mongodb/SQLAlchemy

    应用：
        a. 配置
            app.config['SESSION_TYPE'] = 'redis'
            app.config['SESSION_REDIS'] = Redis(host='192.168.0.94',port='6379')

        b. 替换
            from flask_session import Session
            Session(app)

        注意：session中存储的是字典，修改字典内部元素时，会造成数据不更新。
              - motified = True
              - SESSION_REFRESH_EACH_REQUEST = True and  session.permanent = True(redis中默认)
"""

from flask import Flask, request, session

app = Flask(__name__)
app.secret_key = 'abasd'

# 默认的session存储方式
from flask.sessions import SecureCookieSessionInterface
app.session_interface = SecureCookieSessionInterface()

# 其他
# 方式一：保存到redis中
from redis import Redis
from flask_session import RedisSessionInterface

app.session_interface = RedisSessionInterface(
    redis=Redis(host='127.0.0.1', port=6379),
    key_prefix='rediszz'  # session的前缀
)
# 方式二 保存在redis中
from flask_session import Session
from redis import Redis

app.config['SESSION_TYPE'] = 'REDIS'
app.config['SESSION_REDIS'] = Redis(host='', db='', port='', password='', encoding='')
Session(app)


@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return 'login'
    user = request.form.get('user')
    pwd = request.form.get('pwd')
    session['user_info'] = {'user': user, 'pwd': pwd}

    return 'login'


@app.route('/index', methods=['GET'])
def index():
    user_info = session.get('user_info')
    return 'index'


if __name__ == '__main__':
    app.run()
